<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JSON练习</title>
    <script src="./ajax.js"></script>
    <script type="text/javascript">
      window.onload = function(){
        var oBtn = document.getElementById('btn');
        var oDiv = document.getElementById('div1');
        var oUl = document.createElement('ul');
        oBtn.onclick = function() {
          $ajax({
            method: "get",
            url: "getNews.php",
            success: function(result){
              oUl.innerHTML = "";
              
              var arr = JSON.parse(result);
              
              for(let i=0; i<arr.length; i++){
                var newLi = document.createElement("li");
                var oA = document.createElement('a');
                oA.href = '#'+i;
                oA.innerHTML = arr[i].title;
                var oSpen = document.createElement("span");
                oSpen.innerHTML = `【${arr[i].date}】`;
                
                newLi.appendChild(oA);
                newLi.appendChild(oSpen);
                
                oUl.appendChild(newLi);
              }
              
              oDiv.appendChild(oUl);
            },
            error: function(msg){
              alert(msg);
            }
          });
        }
      }
    </script>
  </head>
  <body>
    <button id="btn">按键</button>
    <div id="div1"></div>
  </body>
</html>
